//
//  XJSelectCityViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/4.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJSelectCityViewController.h"

//Model
#import "XJCityBigModel.h"

@interface XJSelectCityViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;
//@property(nonatomic, strong) UIView *currentCityView; // 当前城市
@property(nonatomic, strong) UILabel *currentCityLabel; // 当前城市


/**数据源*/
@property(nonatomic, strong) NSMutableArray <XJCityBigModel *> *dataArray;

@property(nonatomic, strong) NSMutableArray *indexTitleArr;

@end

@implementation XJSelectCityViewController

#pragma mark - 一 lazy
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)indexTitleArr {
    if (!_indexTitleArr) {
        _indexTitleArr = [NSMutableArray array];
    }
    return _indexTitleArr;
}

- (UITableView *)tableView{
    if (!_tableView) {
        
        CGFloat y = kIs_iPhoneX ? ADAPTHeight(44) : ADAPTHeight(20);
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, KWidth, KHeight - XJTopNavH) style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (UILabel *)currentCityLabel {
    if (!_currentCityLabel) {
        _currentCityLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, KWidth - 100, 44)];
        _currentCityLabel.text = @"杭州";
    }
    return _currentCityLabel;
}

#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"城市选择";
    
    [self readLocalCityJSONData];
    
    [self.view addSubview:self.tableView];
    
    [self setupCuttentCityView];
}
- (void)setupCuttentCityView {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, 44)];
    
    // 当前城市
    UILabel *tipsLabel = [[UILabel alloc] init];
    tipsLabel.text = @"当前城市";
//    tipsLabel.backgroundColor = [UIColor lightGrayColor];
    [headerView addSubview:tipsLabel];
    
    UIImageView *iconIV = [[UIImageView alloc] init];
    iconIV.image = [UIImage imageNamed:@"address_list_off"];
    [headerView addSubview:iconIV];
//    iconIV.backgroundColor = [UIColor redColor];
    
    [headerView addSubview:self.currentCityLabel];
//    self.currentCityLabel.backgroundColor = [UIColor yellowColor];
    
    self.tableView.tableHeaderView = headerView;
    
    // layout
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(80, 44));
        make.left.equalTo(headerView.mas_left).offset(20);
        make.top.equalTo(headerView.mas_top);
    }];
    [iconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(10, 15));
        make.left.equalTo(tipsLabel.mas_right).offset(5);
        make.centerY.equalTo(tipsLabel.mas_centerY);
    }];
    [self.currentCityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconIV.mas_right).offset(5);
        make.top.bottom.right.equalTo(headerView);
    }];
}


// 1.定位功能
// 城市列表

- (void)readLocalCityJSONData {
    // json数据.---->Data --->Dic/Arr/Str
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"addressCity" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
    
    // 转字典
    NSArray *bigArr = [NSJSONSerialization JSONObjectWithData:jsonData options:(NSJSONReadingMutableContainers) error:nil];
    
    self.dataArray = [XJCityBigModel mj_objectArrayWithKeyValuesArray:bigArr];
    
    for (XJCityBigModel *bigModel in self.dataArray) {
        [self.indexTitleArr addObject:bigModel.firstChar];
    }
}

#pragma mark - 三 UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    XJCityBigModel *bigModel = self.dataArray[section];
    return  bigModel.addresses.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCellId"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"UITableViewCellId"];
    }
    
    XJCityBigModel *bigModel = self.dataArray[indexPath.section];
    XJCityModel *smallModel = bigModel.addresses[indexPath.row];
    
    cell.textLabel.text = smallModel.name;
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.dataArray[section].firstChar;
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return self.indexTitleArr;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    // 将城市回到出去并返回。
    if (self.selectedCityBlock) {
        XJCityBigModel *bigModel = self.dataArray[indexPath.section];
        XJCityModel *cityModel = bigModel.addresses[indexPath.row];

        self.selectedCityBlock(cityModel.name);
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
